from unittest import TestCase, TestLoader, TextTestRunner
from pytest import GatewayAFactory
from pyduct.handlers import JsonHandler, classOf
from pyson.gen import compactFormat, prettyFormat, PysonObject, string
from argo.jdom import JdomParser
from java.io import StringReader
from pyduct.mementos import MementoInfoForFieldBuilder
from pyduct.metadata import CommonMetaDataBuilder
from pyduct.validation import CookieValidationBuilder


class Comp1(PysonObject):
    label = string()

class Comp2(Comp1): pass
class Comp3(Comp1): pass
class Diamond(Comp2, Comp3): pass

class TestFragments(TestCase):

    def testDiamond(self):
        d = Diamond()
        d.label = "xyz"
        dBuilder = d.createBuilder()
        output = compactFormat(dBuilder)
        self.assertEquals("{\"label\":\"xyz\"}", output)

    def testNotExtendedHandler(self):
        gateway = GatewayAFactory().newGateway(classOf("pytest.SampleStarterA"))
        handler = JsonHandler()
        cookieMsg = handler.getCookieMessage(gateway)
        self.assertEquals(4, len(cookieMsg.fields))
        self.assertEquals(0, hasattr(cookieMsg.fields[0], "CommonMetaData"))
        self.assertEquals(0, hasattr(cookieMsg.fields[0], "MementoInfoForField"))
        self.assertEquals(0, hasattr(cookieMsg.fields[0], "ValidationInfo"))
        self.assertEquals(0, hasattr(cookieMsg.fields[0], "ListItems"))

    def testHandlerExtendedByMementoInfoForField(self):
        gateway = GatewayAFactory().newGateway(classOf("pytest.SampleStarterA"))
        handler = JsonHandler()
        handler.extensionFragmentBuilders += (MementoInfoForFieldBuilder,)

        cookieMsg = handler.getCookieMessage(gateway)
        self.assertEquals(4, len(cookieMsg.fields))
        self.assertEquals(0, cookieMsg.fields[0].MementoInfoForField.isParameter)
        self.assertEquals(0, cookieMsg.fields[1].MementoInfoForField.isParameter)
        self.assertEquals(0, cookieMsg.fields[2].MementoInfoForField.isParameter)
        self.assertEquals(0, cookieMsg.fields[3].MementoInfoForField.isParameter)

#        cookieMsgBuilder = cookieMsg.createBuilder()
#        output = prettyFormat(cookieMsgBuilder)
#        print output

    def testHandlerExtendedByCommonMetaData(self):
        gateway = GatewayAFactory().newGateway(classOf("pytest.SampleStarterA"))
        handler = JsonHandler()
        handler.extensionFragmentBuilders += (CommonMetaDataBuilder,)

        cookieMsg = handler.getCookieMessage(gateway)
        self.assertEquals(4, len(cookieMsg.fields))
        self.assertEquals("parameter0", cookieMsg.fields[0].CommonMetaData.title)

#        cookieMsgBuilder = cookieMsg.createBuilder()
#        output = prettyFormat(cookieMsgBuilder)
#        print output

    def testHandlerExtendedByCookieValidation(self):
        gateway = GatewayAFactory().newGateway(classOf("pytest.SampleStarterA"))
        handler = JsonHandler()
        handler.extensionFragmentBuilders += (CookieValidationBuilder,)

        cookieMsg = handler.getCookieMessage(gateway)
        self.assertEquals(4, len(cookieMsg.fields))
        self.assertEquals("must be greater than or equal to 10", cookieMsg.fields[0].ValidationInfo.message)
        self.assertEquals(0, cookieMsg.fields[0].ValidationInfo.isValid)

def runTests():
    suite = TestLoader().loadTestsFromTestCase(TestFragments)
    TextTestRunner(verbosity=2).run(suite)
